package myky.com.doctorapp.presenter;

import com.xiangyao.train.utils.RxScedulerHelper;

import java.io.File;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;
import myky.com.doctorapp.base.BaseEntity;
import myky.com.doctorapp.base.BasePresenterIm;
import myky.com.doctorapp.base.RxSubscribe;
import myky.com.doctorapp.base.SimpleSubscribe;
import myky.com.doctorapp.contract.PersonInfoContract;
import myky.com.doctorapp.entity.PersionInfoEntity;
import myky.com.doctorapp.entity.SimpleReponseEntity;
import myky.com.doctorapp.entity.UserEntity;
import myky.com.doctorapp.model.PersonInfoModel;

public class PersonInfoPresenter extends BasePresenterIm<PersonInfoContract.View> implements PersonInfoContract.Presenter {


    PersonInfoModel personInfoModel;

    public PersonInfoPresenter() {
        personInfoModel = new PersonInfoModel();
    }

    @Override

    public void getUserInfo() {


        personInfoModel.getUserInfo().subscribe(new RxSubscribe<UserEntity.PayLoadBean>() {
            @Override
            protected void onSuccess(UserEntity.PayLoadBean persionInfoEntity) {


                mView.hideLoading();
                mView.showDataSuccess(persionInfoEntity);

            }

            @Override
            protected void onFailed(int code, String msg) {
                mView.hideLoading();
                mView.showErrorMessage("code " + code + "," + msg);


            }

            @Override
            public void onSubscribe(Disposable d) {

                addSubscription(d);

                mView.showLoadingView("请稍后");

            }
        });

    }

    @Override
    public void updateUserInfo(String userName, String advanter, String version) {
        personInfoModel.updateUserInfo(userName, advanter, version)
                .compose(RxScedulerHelper.INSTANCE.io_main())
                .filter(new Predicate<BaseEntity<SimpleReponseEntity.PayLoadBean>>() {
                    @Override
                    public boolean test(BaseEntity<SimpleReponseEntity.PayLoadBean> stringBaseEntity) throws Exception {
                        boolean b = stringBaseEntity.type == 3;


                        if (!b) {

                            mView.updateInfoFailed();
                            mView.hideLoading();
                        }

                        return b;
                    }
                }).compose(RxScedulerHelper.INSTANCE.io_main())
                .subscribe(new RxSubscribe<SimpleReponseEntity.PayLoadBean>() {
                    @Override
                    protected void onSuccess(SimpleReponseEntity.PayLoadBean s) {
                        mView.hideLoading();

                        mView.showUpdateResult(userName,advanter);
                    }

                    @Override
                    protected void onFailed(int code, String msg) {

                        mView.logCat(msg);

                        mView.hideLoading();

                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                        addSubscription(d);

                        mView.showLoadingView("请稍后");

                    }
                });
    }

    @Override
    public void upLoadPic(File file) {


        personInfoModel.upLoadPic(file).subscribe(new RxSubscribe<String>() {
            @Override
            protected void onSuccess(String s) {
                mView.hideLoading();

                mView.showUpSuccesMd5(s);

            }

            @Override
            protected void onFailed(int code, String msg) {

                mView.logCat(msg);

                mView.hideLoading();

            }

            @Override
            public void onSubscribe(Disposable d) {

                addSubscription(d);

                mView.showLoadingView("正在上传");

            }
        });

    }
}
